﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

using EHTWebsite.Code;
/// <summary>
/// Author      toanvt
/// CreatedDate  7/12/2011
/// </summary>
public partial class AdminCompanyInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            ThongTinCongTy theThongTinCongTy = ThongTinCongTyProvider.GetThongTinCongTy();

            if (theThongTinCongTy != null)
            {
                this.txtDiaChiOther.Text = theThongTinCongTy.DiaChiOther;
                this.txtDiChiVN.Text = theThongTinCongTy.DiaChiVN;
                this.txtDiDong.Text = theThongTinCongTy.DiDong;
                this.txtDienThoai.Text = theThongTinCongTy.DienThoai;
                this.txtFax.Text = theThongTinCongTy.Fax;
                this.txtEmail.Text = theThongTinCongTy.Email;
                this.txtMaSoThue.Text = theThongTinCongTy.MaSoThue;
                this.txtEmailPassword.Attributes.Add("value", theThongTinCongTy.EmailPassword);

                try
                {
                    this.txtNgayThanhLap.Text = theThongTinCongTy.NgayThanhLap.ToString("dd/MM/yyyy");
                }
                catch
                {

                }

                this.txtSologanOther.Text = theThongTinCongTy.SologanOther;
                this.txtSologanVN.Text = theThongTinCongTy.SologanVN;
                this.txtTaiKhoan.Text = theThongTinCongTy.TaiKhoan;
                this.txtTenCongTyOther.Text = theThongTinCongTy.TenCongTyOther;
                this.txtTenCongTyVN.Text = theThongTinCongTy.TenCongTyVN;
                this.txtWebsite.Text = theThongTinCongTy.Website;
                this.fckMoTaOther.Value = theThongTinCongTy.MoTaOther;
                this.fckMoTaVN.Value = theThongTinCongTy.MoTaVN;
                this.fckFooterVN.Value = theThongTinCongTy.FooterVN;
                this.fckFooterOther.Value = theThongTinCongTy.FooterOther;
                this.imgLogo.ImageUrl = "../ImageDatas/Logos/" + theThongTinCongTy.LogoUrl;
                ViewState["LogoUrl"] = theThongTinCongTy.LogoUrl;
            }

            // ẩn các validation cho các control Other Language
            if (HangSo.USING_MULTIL_LANGUAGE == false)
            {
                rvDiaChiOther.Enabled = false;
                rvTenCongTyOther.Enabled = false;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Page.Validate();

        if (Page.IsValid)
        {
            ThongTinCongTy theThongTinCongTy = new ThongTinCongTy();
            theThongTinCongTy.CongTyID = HangSo.COMPANY_ID;
            theThongTinCongTy.DiaChiOther = this.txtDiaChiOther.Text.Trim();
            theThongTinCongTy.DiaChiVN = this.txtDiChiVN.Text.Trim();
            theThongTinCongTy.DiDong = this.txtDiDong.Text.Trim();
            theThongTinCongTy.DienThoai = this.txtDienThoai.Text.Trim();
            theThongTinCongTy.Email = this.txtEmail.Text.Trim();
            theThongTinCongTy.EmailPassword = this.txtEmailPassword.Text;
            theThongTinCongTy.MaSoThue = this.txtMaSoThue.Text.Trim();
            theThongTinCongTy.MoTaOther = this.fckMoTaOther.Value;
            theThongTinCongTy.MoTaVN = this.fckMoTaVN.Value;
            theThongTinCongTy.Fax = this.txtFax.Text.Trim();
            theThongTinCongTy.FooterOther = this.fckFooterOther.Value;
            theThongTinCongTy.FooterVN = this.fckFooterVN.Value;

            try
            {
                theThongTinCongTy.NgayThanhLap = DateTime.Parse(this.txtNgayThanhLap.Text.Trim());
            }
            catch
            {
                theThongTinCongTy.NgayThanhLap = DateTime.Now;
            }

            theThongTinCongTy.SologanOther = this.txtSologanOther.Text.Trim();
            theThongTinCongTy.SologanVN = this.txtSologanVN.Text.Trim();
            theThongTinCongTy.TaiKhoan = this.txtTaiKhoan.Text.Trim();
            theThongTinCongTy.TenCongTyOther = this.txtTenCongTyOther.Text.Trim();
            theThongTinCongTy.TenCongTyVN = this.txtTenCongTyVN.Text.Trim();
            theThongTinCongTy.Website = this.txtWebsite.Text.Trim();
            theThongTinCongTy.LogoUrl = ViewState["LogoUrl"].ToString();

            if (fuPictureUrl.HasFile)
            {

                String fileName = fuPictureUrl.PostedFile.FileName;
                String fileType = Path.GetExtension(fileName);
                String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                fuPictureUrl.PostedFile.SaveAs(Server.MapPath("~/") + "//ImageDatas//Logos//" + newFileName);
                this.imgLogo.ImageUrl = "..//ImageDatas//Logos//" + newFileName;
                theThongTinCongTy.LogoUrl = newFileName;
            }

            ThongTinCongTyProvider.IUThongTinCongTys(theThongTinCongTy, false);
            this.txtEmailPassword.Attributes.Add("value", theThongTinCongTy.EmailPassword);
            this.ltrMessage.Text = "Cập nhật thành công";
        }
    }
}
